package com.hwz.hwzliving.commodity.service.impl;

import com.hwz.hwzliving.commodity.dao.AttrAttrgroupRelationDao;
import com.hwz.hwzliving.commodity.entity.AttrAttrgroupRelationEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.Query;

import com.hwz.hwzliving.commodity.dao.AttrDao;
import com.hwz.hwzliving.commodity.entity.AttrEntity;
import com.hwz.hwzliving.commodity.service.AttrService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Transactional
    @Override
    public void saveAttrAndRelation(AttrEntity attr) {
        //完成基本数据的保存
        this.save(attr);
        //保存与属性分组之间的关系
        if (attr.getAttrType() == 1 && attr.getAttrGroupId() != null){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attr.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long categoryId) {
        //查询基础属性
        QueryWrapper<AttrEntity> attrEntityQueryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type", 1);
        //如果category不为0就携带查询条件
        if (categoryId != 0){
            attrEntityQueryWrapper.eq("category_id", categoryId);
        }
        String key = (String)params.get("key");
        if (StringUtils.isNotBlank(key)){
            attrEntityQueryWrapper.and((wapper) -> {
                wapper.eq("attr_id", key).or().like("attr_name", key);
            });
        }



        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                attrEntityQueryWrapper

        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils querySaleAttrPage(Map<String, Object> params, Long categoryId) {
        //查询销售属性
        QueryWrapper<AttrEntity> attrEntityQueryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type", 0);
        //如果category不为0就携带查询条件
        if (categoryId != 0){
            attrEntityQueryWrapper.eq("category_id", categoryId);
        }
        String key = (String)params.get("key");
        if (StringUtils.isNotBlank(key)){
            attrEntityQueryWrapper.and((wapper) -> {
                wapper.eq("attr_id", key).or().like("attr_name", key);
            });
        }



        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                attrEntityQueryWrapper

        );
        return new PageUtils(page);
    }

}